#include "cocos2d.h"
#include "Director/AIDirector.h"
#include "Director/PlayerPerformance.h"
#include "Levels/CatSpawnSet.h"

#include "config.h"
#include "src/TestMacros.h"
#include "src/CheckMacros.h"
#include "src/TestRunner.h"
#include "src/TimeConstraint.h"
#include "src/ReportAssert.h"

using namespace cocos2d;
using namespace UnitTest;

SUITE(AIDirector)
{

	TEST(AIDirectorInitialized)
	{
		CCArray* spawnSet = new CCArray();
		AIDirector* myAIDirector = new AIDirector(0, 0, spawnSet, 0);

		bool isDirectorInitialized = myAIDirector->getPeriod() != 0;
		CHECK_EQUAL(isDirectorInitialized, true);

		//CCLog("Test %f | %i", myAIDirector->getPeriod(), isDirectorInitialized);

		//cleanup
		delete myAIDirector;
		myAIDirector = 0;
		spawnSet->release();
		spawnSet = 0;
	}

	TEST(AIDirectorUpdateIncreaseDifficulty)
	{
		//Update 500 seconds.	Difficulty is increased. Values in difficulty factor is increased.
		int factors = 4;
		int updatedValueArray[factors];

		CCArray* spawnSet = new CCArray();
		PlayerPerformance* myPlayerPerformance = new PlayerPerformance(500.0);
		AIDirector* myAIDirector = new AIDirector(0, 100, spawnSet, myPlayerPerformance);

		for(int i=0; i<factors; i++)
		{
			updatedValueArray[i] = myAIDirector->getDifficultyFactorDifficulty(i);
		}

		for(int i=0; i<10; i++)
		{
			myPlayerPerformance->update(10.0);
			myAIDirector->update(10.0);
		}

		for(int i=0; i<factors; i++)
		{
			bool isIncreased = myAIDirector->getDifficultyFactorDifficulty(i) > updatedValueArray[i];
			CHECK_EQUAL(isIncreased, true);
		}

		//CCLog("Test AIDirectorUpdateIncreaseDifficulty: %i | %i", myAIDirector->getCurrentDifficulty(), myPlayerPerformance->getDifficultyLevel());

		//cleanup
		delete myAIDirector;
		myAIDirector = 0;
		delete myPlayerPerformance;
		myPlayerPerformance = 0;
		spawnSet->release();
		spawnSet = 0;
	}

	TEST(AIDirectorUpdateDecreaseDifficulty)
	{
		// Call PlayerPerformance to reduce difficulty. Update 50 seconds.	Difficulty is decreased. Properties in difficulty factor is decreased.
		int minDifficulty = 1;
		int maxDifficulty = 200;
		int factors = 4;
		int updatedValueArray[factors];

		CCArray* spawnSet = new CCArray();
		PlayerPerformance* myPlayerPerformance = new PlayerPerformance(50.0);
		AIDirector* myAIDirector = new AIDirector(minDifficulty, maxDifficulty, spawnSet, myPlayerPerformance);

		int currDifficulty = myAIDirector->getCurrentDifficulty();

		for(int i=0; i<factors; i++)
		{
			updatedValueArray[i] = myAIDirector->getDifficultyFactorDifficulty(i);
		}

		for(int i=0; i<10; i++)
		{
			myPlayerPerformance->reduceDifficulty();
			myAIDirector->update(50.0);
		}

		// Difficulty should decrease
		bool difficultyDecreased = myAIDirector->getCurrentDifficulty() < currDifficulty;
		CHECK_EQUAL(difficultyDecreased, true);

		for(int i=0; i<factors; i++)
		{
			bool isDecreased = myAIDirector->getDifficultyFactorDifficulty(i) < updatedValueArray[i];
			CHECK_EQUAL(isDecreased, true);
		}

		//cleanup
		delete myAIDirector;
		myAIDirector = 0;
		delete myPlayerPerformance;
		myPlayerPerformance = 0;
		spawnSet->release();
		spawnSet = 0;
	}

	TEST(AIDirectorUpdatePeriod)
{
	int minDifficulty = 1;
	int maxDifficulty = 10;

	CCArray* spawnSet = new CCArray();
	PlayerPerformance* myPlayerPerformance = new PlayerPerformance(50.0);
	AIDirector* myAIDirector = new AIDirector(minDifficulty, maxDifficulty, spawnSet, myPlayerPerformance);

	int currDifficulty = myAIDirector->getCurrentDifficulty();

	myAIDirector->update(1.0);

	// Difficulty should remain constant
	CHECK_EQUAL(myAIDirector->getCurrentDifficulty(), currDifficulty);

	//cleanup
	delete myAIDirector;
	myAIDirector = 0;
	delete myPlayerPerformance;
	myPlayerPerformance = 0;
	spawnSet->release();
	spawnSet = 0;
}

}




